package com.github.postsanf.yinian.adapter;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.ImageOptHelper;

import java.util.List;

public class TimeMarksMorePicAdapter extends BaseAdapter {

    private Context context;
    private List<String> coverPic;

    public TimeMarksMorePicAdapter(Context context, List<String> coverPic) {
        this.context = context;
        this.coverPic = coverPic;
        for (int i = 0; i < 6; i++) {
            coverPic.remove(i);
        }
    }

    @Override
    public int getCount() {
        return coverPic.size();
    }

    @Override
    public Object getItem(int position) {
        return coverPic.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.more_pic_item, null);
            viewHolder = new ViewHolder();
            viewHolder.sd_more_pic = ((SimpleDraweeView) convertView.findViewById(R.id.sd_more_pic));
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        int screenWidthPixels = DisplayUtils.getScreenWidthPixels((Activity) context);
        int itemWidth = (screenWidthPixels) / 2;
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(itemWidth, (int) (1.5 * itemWidth));
        viewHolder.sd_more_pic.setLayoutParams(params);
        ImageOptHelper.showFrescoImg(Uri.parse(coverPic.get(position)), viewHolder.sd_more_pic, context);
        viewHolder.sd_more_pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.setOnClickSelectedListener(coverPic.get(position));
                }
            }
        });
        return convertView;
    }

    class ViewHolder {
        SimpleDraweeView sd_more_pic;
    }

    private OnClickSelectedListener listener;

    public interface OnClickSelectedListener {
        void setOnClickSelectedListener(String url);
    }

    public void setOnClickSelectedListener(OnClickSelectedListener listener) {
        this.listener = listener;
    }
}
